package com.yanqu.road.server.cmd.activity.busyfarming;

import com.yanqu.road.entity.activity.busyfarming.BusyFarmingUserData;
import com.yanqu.road.entity.activity.busyfarming.enums.eBusyFarmingLandState;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.BusyFarmingProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.busyfarming.BusyFarmingModule;
import com.yanqu.road.server.gameplayer.module.activity.busyfarming.BusyFarmingPb;
import com.yanqu.road.server.manger.activity.busyfarming.BusyFarmingMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_BUSY_FARMING_STEAL_SEARCH_DETAIL_FROM_CROSS,desc = "偷菜-果园详情-跨服返回")
public class BusyFarmingStealHomeDetailCmdFromCross extends AbstractCommand {

    long userId;
    int crossRet;
    long serverId;
    BusyFarmingProto.CrossBusyFarmingStealDataTemp stealDataTemp;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BusyFarmingProto.CrossBusyFarmingStealHomeDetailRespMsg respMsg = BusyFarmingProto.CrossBusyFarmingStealHomeDetailRespMsg.parseFrom(packet.getBytes());
        userId = respMsg.getUserId();
        crossRet = respMsg.getRet();
        serverId = respMsg.getServerId();
        if(respMsg.hasStealData()){
            stealDataTemp = respMsg.getStealData();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        BusyFarmingModule busyFarmingModule = player.getModule(BusyFarmingModule.class);
        BusyFarmingUserData userData = busyFarmingModule.getUserData();
        BusyFarmingProto.BusyFarmingStealHomeDetailRespMsg.Builder respMsg = BusyFarmingProto.BusyFarmingStealHomeDetailRespMsg.newBuilder();
        if(userData != null){
            boolean canSteal;
            synchronized (userData) {
                if (crossRet == -1) {
                    //跨服无数据
                    BusyFarmingProto.BusyFarmingUserDataTemp.Builder builder = BusyFarmingProto.BusyFarmingUserDataTemp.newBuilder();
                    if (busyFarmingModule.stealSearchTempList != null) {
                        for (BusyFarmingProto.CrossBusyFarmingStealDataTemp dataTemp : busyFarmingModule.stealSearchTempList) {
                            if (dataTemp.getUserId() == userId) {
                                stealDataTemp = dataTemp;
                                break;
                            }
                        }
                        //使用缓存(展示家园),移除已成熟
                        builder.setUserId(stealDataTemp.getUserId());
                        builder.setHomeGrade(stealDataTemp.getGrade());
                        builder.setUserBaseInfo(stealDataTemp.getUserBaseInfo());
                        builder.setStealTimes(0);
                        builder.setBeStealTimes(0);
                        builder.setWateringTimes(0);
                        builder.setBeWateringTimes(0);
                        builder.setFruitScore(0);
                        for(BusyFarmingProto.BusyFarmingLandTemp landTemp :stealDataTemp.getLandList()){
                            BusyFarmingProto.BusyFarmingLandTemp.Builder tempBuilder =  landTemp.toBuilder();
                            if(tempBuilder.getStatesList().contains(eBusyFarmingLandState.Harvest.getIntValue())){
                                tempBuilder.setGoodsId(0);
                                tempBuilder.clearStates();
                            }
                            builder.addLand(tempBuilder);
                        }
                        respMsg.setEnemy(builder);
                    }
                    canSteal = false;
                    //归还次数
                    userData.setStealTimes(userData.getStealTimes() - 1);
                    busyFarmingModule.clearStealCacheData();
                } else {
                    //有数据
                    canSteal = true;
                    cacheStealData(busyFarmingModule, stealDataTemp);
                    respMsg.setEnemy(busyFarmingModule.stealTempData);
                }
            }
            if(!canSteal){
                //跨服通知被偷者
                //上跨服中转
                BusyFarmingProto.CrossBusyFarmingStealNotifyMsg.Builder notifyMsg  = BusyFarmingProto.CrossBusyFarmingStealNotifyMsg.newBuilder();
                notifyMsg.setActivityId(BusyFarmingMgr.getShowTimeActivityId());
                notifyMsg.setUserId(stealDataTemp.getUserId());
                notifyMsg.setServerId(stealDataTemp.getUserBaseInfo().getServerId());
                notifyMsg.setLandId(stealDataTemp.getLandList().get(0).getId());
                notifyMsg.setSeedId(0);
                notifyMsg.setNum(0);
                notifyMsg.setEnemyUserId(player.getUserId());
                UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId());
                notifyMsg.setEnemy(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                player.sendPacket(Protocol.C_BUSY_FARMING_ACTIVITY_STEAL_NOTIFY, notifyMsg);
            }
            respMsg.setRet(0);
            respMsg.setCanSteal(canSteal);
            player.sendPacket(Protocol.U_BUSY_FARMING_STEAL_HOME_DETAIL, respMsg);
        }

    }

    public void cacheStealData(BusyFarmingModule busyFarmingModule, BusyFarmingProto.CrossBusyFarmingStealDataTemp stealDataTemp) {
        //缓存待偷取的数据
        BusyFarmingProto.BusyFarmingUserDataTemp.Builder stealTempData = BusyFarmingPb.parseBusyFarmingUserDataBuilder(stealDataTemp);
        busyFarmingModule.stealTempData = stealTempData;
        busyFarmingModule.clearStealCacheData();
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
